<!DOCTYPE html>
{% load static %}
<html lang="ch">
<head>
    <meta charset="UTF-8">
    <title>用户注册</title>
    <link rel="stylesheet" type="text/css" href="{% static 'css/register.css' %}">
    <link rel="icon" type="image/x-icon" href="{% static 'img/icon.ico' %}">
    <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
</head>
<body>
<div class="re_div">
    <div class="cLeft">
        <p id="p1">新用户注册</p>
        <p id="p2">USER REGISTER</p>
    </div>
    <div class="cCenter">
        <form method="post" action="{% url 'register' %}" id='register'>
            {% csrf_token %}
            <table>
                <tr>
                    <td class="td_left"><label for="username">
                        用户名<span class="Required-fields"> *</span>
                    </label></td>
                    <td class="td_right">
                        {{ form.username }}
                    </td>
                </tr>

                <tr>
                    <td class="td_left"><label for="password">
                        密码<span class="Required-fields"> *</span>
                    </label></td>
                    <td class="td_right">
                        {{ form.password }}
                    </td>
                </tr>

                <tr>
                    <td class="td_left"><label for="repassword">
                        请重复密码<span class="Required-fields"> *</span></label>
                    </td>
                    <td class="td_right">
                        {{ form.repassword }}
                    </td>
                </tr>

                <tr>
                    <td class="td_left"><label for="name">
                        昵称<span class="Required-fields"> *</span>
                    </label></td>
                    <td class="td_right">
                        {{ form.nickname }}
                    </td>
                </tr>

                <tr>
                    <td class="td_left"><label for="email">
                        Email<span class="Required-fields"> *</span>
                    </label></td>
                    <td class="td_right">
                        <input type="email" name="email" id="email" placeholder="请输入邮箱">
                        <button type="button" id="sendEmailCode">发送验证码</button>
                    </td>
                </tr>

{#                邮箱验证码输入框#}
                <tr>
                    <td class="td_left"><label for="email_code">邮箱验证码<span class="Required-fields"> *</span></label></td>
                    <td class="td_right"><input type="text" name="email_code" id="email_code" placeholder="请输入邮箱验证码"></td>
                </tr>

                <tr>
                    <td class="td_left"><label for="tel">手机号</label></td>
                    <td class="td_right"><input type="text" name="tel" id="tel" placeholder="请输入手机号"></td>
                </tr>

{#                <tr>#}
{#                    <td class="td_left"><label>性别</label></td>#}
{#                    <td class="td_right">#}
{#                        <input type="radio" name="gender" value="male"> 男#}
{#                        <input type="radio" name="gender" value="female"> 女#}
{#                    </td>#}
{#                </tr>#}

                <tr>
                    <td class="td_left"><label for="birthday">出生日期</label></td>
                    <td class="td_right"><input type="date" name="birthday" id="birthday" placeholder="请输入出生日期"></td>
                </tr>

                <!-- <tr>
                    <td class="td_left"><label for="checkcode" >验证码</label></td>
                    <td class="td_right"><input type="text" name="checkcode" id="checkcode" placeholder="请输入验证码">
                        <img id="cheImg" src="img/verify_code.jpg">
                    </td>
                </tr>-->
                <tr>
                    <td colspan="2" align="center"><input id="btn_sub" type="submit" value="注册" style="cursor: pointer;"></td>
                </tr>
            </table>

            <!-- 错误信息模板位置 -->
            <p class="error">{{ error_message }}</p>
        </form>


    </div>
    <div class="cRight">
        <p id="p3">已有账号？<a href="/user/login">立即登录</a></p>
    </div>

</div>
</body>
<script>
    document.getElementById('sendEmailCode').addEventListener('click', function() {
        console.log('捕获事件');

        // 获取用户填写的邮箱
        var userEmail = document.getElementById('email').value;

        // 进行邮箱格式检查
        if (!validateEmail(userEmail)) {
            alert('请输入有效的邮箱地址！');
            return;
        }

        // 调用后台发送邮件的函数
        sendEmailCode(userEmail);
    });

    function validateEmail(email) {
        console.log('邮箱检查');

        // 简单的邮箱格式检查
        var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
        return emailRegex.test(email);
    }

    function sendEmailCode(email) {
        console.log('调用后台发送消息');

        var csrf_token = $('[name="csrfmiddlewaretoken"]').val();
        var sendEmailButton = $('#sendEmailCode');
        var countdown = 60;

        // 禁用按钮，并显示倒计时
        sendEmailButton.prop('disabled', true);

        function updateCountdown() {
            sendEmailButton.text('重新发送(' + countdown + 's)');
            countdown--;

            if (countdown < 0) {
                clearInterval(timer);
                sendEmailButton.text('发送验证码');
                sendEmailButton.prop('disabled', false);  // 启用按钮
            }
        }

        // 更新倒计时，并每秒执行一次
        var timer = setInterval(updateCountdown, 1000);

        $.ajax({
            url: '/user/send_email_code/',  // 替换成你的正确路径
            type: 'POST',
            data: {
                'email': email,
                'csrfmiddlewaretoken': csrf_token,
            },
            success: function(response) {
                // 处理后台返回的响应
                {#console.log('邮件验证码发送成功，后台响应：', response);#}
                {#alert('验证码已发送至您的邮箱，请查收！');#}
                if (response.status === 'success') {
                    alert('验证码已发送至您的邮箱，请查收！');
                } else {
                    alert('该邮箱已被注册或邮箱有误！');
                    clearInterval(timer);  // 立即解除倒计时
                    sendEmailButton.text('发送验证码');
                    sendEmailButton.prop('disabled', false);  // 启用按钮
                }
            },
            error: function(error) {
                // 处理发送失败的情况
                console.error('发送验证码失败，错误信息：', error);
                alert('发送验证码失败，请稍后重试！');
                clearInterval(timer);  // 立即解除倒计时
                sendEmailButton.text('发送验证码');
                sendEmailButton.prop('disabled', false);  // 启用按钮
            }
        });
    }


</script>
</html>
